package com.wocao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wocao.entity.ResponseResult;
import com.wocao.entity.SysDictData;
import com.wocao.entity.SysDictType;
import com.wocao.entity.SysDictTypeAndDateCheck;
import com.wocao.service.SysDictTypeService;
import com.wocao.utils.log.OperLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
public class SysDictTypeController {

    @Autowired
    private SysDictTypeService sysDictTypeService;
    /*分页显示：查询*/
    @PostMapping("sysDictType-find")
    public ResponseResult<Page<SysDictType>> findSysDictType(@RequestBody SysDictTypeAndDateCheck checkData) {
        try {
            Page<SysDictType> pages = sysDictTypeService.getPage(new Page<>(Long.parseLong(checkData.getCurrent()), Long.parseLong(checkData.getSize()))
                    , checkData);
            /*使用xml完成多条件查询*/
            return ResponseResult.ok(pages);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500L, "查询失败" + e.getMessage());
        }
    }

    /*获取所有数据*/
    @GetMapping("sysDictType-all")
    public ResponseResult<List<SysDictType>> getSysDictDataPages(){
        try{
            List<SysDictType> list = sysDictTypeService.list();
            return new ResponseResult<>(200L,"分页查询成功",list);
        }catch (Exception e){
            return new ResponseResult<>(500L,"分页失败"+e.getMessage(),null);
        }
    }

    /*删除一条*/
    @OperLog(decideBusinessType = "1")
    @DeleteMapping("SysDictType/{id}")
    public ResponseResult<String> deleteSysDictType(@PathVariable Long id){
        try {
            sysDictTypeService.removeById(id);
            return ResponseResult.ok("ok");
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error(500L,"删除失败");
        }
    }

    /*删除多条*/
    @OperLog(decideBusinessType = "3")
    @PostMapping("SysDictType-some")
    public ResponseResult<String> deleteSomeSysDictType(@RequestBody String[] ids){
        try {
            int[] idArray = Arrays.stream(ids).mapToInt(Integer::parseInt).toArray();
            List<Integer> collect = Arrays.stream(idArray).boxed().collect(Collectors.toList());
            System.out.println(collect);
            boolean b = sysDictTypeService.removeByIds(collect);
            System.out.println(b);
            return ResponseResult.ok("ok");
        }catch (Exception e){
            return ResponseResult.error(500L,"删除失败");
        }
    }

    /*添加一条数据*/
    @OperLog(decideBusinessType = "1")
    @PostMapping("sysDictType")
    public ResponseResult<String> addSysDictType(@RequestBody SysDictType sysDictType){
        try {
            sysDictType.setCreateTime(LocalDateTime.now());
            sysDictTypeService.save(sysDictType);
            return ResponseResult.ok("ok");
        }catch (Exception e){
            return ResponseResult.error(500L,"添加失败");
        }
    }

    /*修改一条数据*/
    @OperLog(decideBusinessType = "2")
    @PutMapping("sysDictType")
    public ResponseResult<String> updateSysDictType(@RequestBody SysDictType sysDictType){
        try {
            sysDictTypeService.updateById(sysDictType);
            return ResponseResult.ok("ok");
        }catch (Exception e){
            return ResponseResult.error(500L,"修改失败");
        }
    }

    /*导出*/
    @OperLog(decideBusinessType = "5")
    @PostMapping("sysDictType-exportNumber")
    public ResponseResult<List<SysDictType>> exportNumber(@RequestBody String[] ids){
        List<SysDictType> sysDictTypes ;
        try {
            if (ids == null){
                sysDictTypes = sysDictTypeService.list();
            }else {
                sysDictTypes = sysDictTypeService.listByIds(Arrays.asList(ids));
            }
            return ResponseResult.ok(sysDictTypes);
        }catch (Exception e){
            return ResponseResult.error(500L,"获取导出数据失败" + e.getMessage());
        }
    }
}